﻿using Drive.DbAccess;
using System.Collections.Generic;
using System.Data.Common;

namespace LA.Extensions
{
    public static class ConnectionExtension
    {
        public static DbConnection EnsureOpen(this DbConnection connection)
        {
            if (connection.State != System.Data.ConnectionState.Open)
                connection.Open();
            return connection;
        }
        /// <summary>
        /// 连接需要提前打开，而且执行完毕后需要关闭
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="trans"></param>
        /// <param name="sql"></param>
        /// <param name="dbParameter"></param>
        /// <returns></returns>
        public static DTable ExecuteDTable(this DbConnection conn, DbTransaction trans, string sql, params DbParameter[] dbParameter)
        {
            using (var cmd = conn.CreateCommand())
            {
                if (trans != null)
                {
                    cmd.Transaction = trans;
                }
                cmd.CommandText = sql;
                DTable dt = new DTable();
                bool isNameFileld = false;
                cmd.Parameters.AddRange(dbParameter);
                using (var dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        if (!isNameFileld)
                        {
                            for (int i = 0; i < dr.FieldCount; i++)
                            {
                                dt.Columns.Add(new DColumn { Type = dr.GetFieldType(i), Name = dr.GetName(i) });
                            }
                            isNameFileld = true;
                        }
                        var row = new DRow();
                        var items = new List<DItem>(dr.FieldCount);
                        for (int i = 0; i < dr.FieldCount; i++)
                        {
                            items.Add(new DItem { Column = dt.Columns[i], Value = dr.GetValue(i) });
                        }
                        row.ItemArray = items;
                        dt.Rows.Add(row);
                    }
                    dt.Sql = sql;
                    return dt;
                }
            }
        }
        public static DTable ExecuteDTable(this DbConnection conn, string sql, params DbParameter[] dbParameter)
        {
            var connection = conn.EnsureOpen();
            try
            {
                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandText = sql;
                    DTable dt = new DTable();
                    bool isNameFileld = false;
                    cmd.Parameters.AddRange(dbParameter);
                    var dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        if (!isNameFileld)
                        {
                            for (int i = 0; i < dr.FieldCount; i++)
                            {
                                dt.Columns.Add(new DColumn { Type = dr.GetFieldType(i), Name = dr.GetName(i) });
                            }
                            isNameFileld = true;
                        }
                        var row = new DRow();
                        var items = new List<DItem>(dr.FieldCount);
                        for (int i = 0; i < dr.FieldCount; i++)
                        {
                            items.Add(new DItem { Column = dt.Columns[i], Value = dr.GetValue(i) });
                        }
                        row.ItemArray = items;
                        dt.Rows.Add(row);
                    }
                    dt.Sql = sql;
                    return dt;
                }
            }
            finally
            {
                connection.Close();
            }
        }
    }
}